package com.block.api.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.CommonParam;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.datapush.form.service.IFormInfoPushRecordService;
import com.block.db.entity.AppEncrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 后台定时任务 调用接口
 *
 */
@RestController
@RequestMapping("api/admin/task")
@Validated
public class AdminTaskController {

    @Autowired
    IFormInfoPushRecordService formInfoPushRecordService;


    /**
     * 定时任务
     * 查询 form 推送任务的状态
     *
     */
    @RequestMapping(value = "/runFormPushTask", method = {RequestMethod.POST, RequestMethod.GET})
    public String queryFormPushTask(@RequestAttribute(value = "tranData") JSONObject tranData,
                                    @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                    @RequestAttribute(value = "uri") String uri,
                                    @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                    @RequestAttribute(value = "startDate") Date startdate,
                                    @RequestAttribute(value = "ip") String ip) throws ApiException {

        JSONArray recordIds = tranData.getJSONArray("recordIds");
        formInfoPushRecordService.queryExecutePushRecord(recordIds);

        return ApiUtil.dealReturnJson(new ResponseVo(AppRspCodeConstant.SUCCESS), appEncrypt, "1.0", startdate, uri);
    }


}
